package com.hsteach.service.visit;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.DateUtils;
import com.hsteach.dao.domain.visit.*;
import com.hsteach.dao.mapper.visit.*;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.enums.VisitDataTypeEnum;
import com.hsteach.enums.VisitImgTypeEnum;
import com.hsteach.web.controller.visit.sdk.dto.SdkListRecordDto;
import com.hsteach.web.controller.visit.sdk.vo.SdkFamilyPersonVo;
import com.hsteach.web.controller.visit.sdk.vo.SdkFamilyVo;
import com.hsteach.web.controller.visit.sdk.vo.SdkListRecordVo;
import com.hsteach.web.controller.visit.sdk.vo.SdkRecordDetailVo;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈家访外部接口〉
 *
 * @author ext.qiufukang1
 * @create 2023/11/21
 * @since 1.0.0
 */
@Service
public class VisitSdkService {

    @Autowired
    private VisitRecordMapper visitRecordMapper;

    @Autowired
    private VisitPeersMapper visitPeersMapper;

    @Autowired
    private VisitFamilyInfoMapper visitFamilyInfoMapper;

    @Autowired
    private VisitFamilyPersonMapper visitFamilyPersonMapper;

    @Autowired
    private VisitRecordTextMapper visitRecordTextMapper;

    @Autowired
    private VisitImgMapper visitImgMapper;

    public Long sumRecord(String orgCode) {
        VisitRecord query = new VisitRecord();
        query.setOrgCode(orgCode);
        query.setIsDraft(BooleanEnum.FALSE.getCode());
        query.setVisitDateBegin(DateUtils.getFirstDayOfMonth(DateUtils.getNowyyyyMMdd()));
        query.setVisitDateEnd(DateUtils.getNextFirstDayOfMonth(DateUtils.getNowyyyyMMdd()));
        return visitRecordMapper.selectCount(query);
    }

    public TableDataInfo<SdkListRecordVo> pageQuery(SdkListRecordDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        VisitRecord queryAll = new VisitRecord();
        queryAll.setOrgCode(dto.getOrgCode());
        queryAll.setIsDraft(BooleanEnum.FALSE.getCode());
        queryAll.setPersonName(dto.getSearchName());
        queryAll.setClassId(dto.getClassId());
        if (dto.getSearchDate() != null) {
            queryAll.setVisitDateBegin(DateUtils.getFirstDayOfMonth(dto.getSearchDate()));
            queryAll.setVisitDateEnd(DateUtils.getNextFirstDayOfMonth(dto.getSearchDate()));
        }
        PageHelper.orderBy("id desc");
        List<VisitRecord> allVisitRecords = visitRecordMapper.selectVisitRecordList(queryAll);

        long total = new PageInfo(allVisitRecords).getTotal();

        List<SdkListRecordVo> rows = allVisitRecords.stream().map(e -> {
            SdkListRecordVo vo = new SdkListRecordVo();
            BeanUtils.copyProperties(e, vo);
            vo.setRecordId(e.getId());
            return vo;
        }).collect(Collectors.toList());
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(rows);
        return tableDataInfo;
    }

    public SdkRecordDetailVo detail(Long recordId) {
        VisitRecord record = visitRecordMapper.selectVisitRecordById(recordId);
        if (record == null) {
            throw new ServiceException("未查找到家访记录");
        }
        SdkRecordDetailVo vo = new SdkRecordDetailVo();
        BeanUtils.copyProperties(record, vo);
        vo.setRecordId(recordId);
        VisitImg imgQuery = new VisitImg();
        imgQuery.setRecordId(recordId);
        VisitRecordText text = visitRecordTextMapper.selectVisitRecordTextByRecordId(recordId);
        List<VisitImg> visitImgs = visitImgMapper.selectVisitImgList(imgQuery);

        if (text != null) {
            BeanUtils.copyProperties(text, vo);
        }
        List<VisitImg> visit = visitImgs.stream().filter(e -> VisitImgTypeEnum.VISIT.getCode().equals(e.getImgType())).collect(Collectors.toList());
        vo.setVisitImg(visit.stream().map(VisitImg::getUrl).collect(Collectors.toList()));

        VisitPeers query = new VisitPeers();
        query.setRecordId(recordId);
        List<VisitPeers> visitPeers = visitPeersMapper.selectVisitPeersList(query);
        vo.setVisitPeers(visitPeers);

        VisitFamilyInfo familyQuery = new VisitFamilyInfo();
        familyQuery.setPersonId(record.getPersonId());
        familyQuery.setDataType(VisitDataTypeEnum.RECORD.getCode());
        familyQuery.setRecordId(recordId);
        VisitFamilyInfo recordFamilyInfo = visitFamilyInfoMapper.selectVisitFamilyInfoList(familyQuery).stream().findFirst().orElse(null);
        if (recordFamilyInfo != null) {
            vo.setStructure(recordFamilyInfo.getStructure());
            vo.setDescribeText(recordFamilyInfo.getDescribeText());
        }

        VisitFamilyPerson personQuery = new VisitFamilyPerson();
        personQuery.setPersonId(record.getPersonId());
        personQuery.setDataType(VisitDataTypeEnum.RECORD.getCode());
        personQuery.setRecordId(recordId);
        List<VisitFamilyPerson> visitFamilyPeople = visitFamilyPersonMapper.selectVisitFamilyPersonList(personQuery);
        List<SdkFamilyPersonVo> familyPersons = visitFamilyPeople.stream().map(e -> {
            SdkFamilyPersonVo person = new SdkFamilyPersonVo();
            BeanUtils.copyProperties(e, person);
            return person;
        }).collect(Collectors.toList());

        vo.setFamilyPersons(familyPersons);

        return vo;

    }

    public List<SdkFamilyVo> listFamily(List<String> personIds, List<String> structures, String orgCode) {
        if (CollectionUtils.isEmpty(personIds) && CollectionUtils.isEmpty(structures)) {
            return Lists.newArrayList();
        }
        VisitFamilyInfo query = new VisitFamilyInfo();
        query.setPersonIds(personIds);
        query.setStructures(structures);
        query.setDataType(VisitDataTypeEnum.STUDENT.getCode());
        query.setOrgCode(orgCode);
        List<VisitFamilyInfo> visitFamilyInfos = visitFamilyInfoMapper.selectVisitFamilyInfoList(query);
        List<SdkFamilyVo> ret = visitFamilyInfos.stream().map(e -> {
            SdkFamilyVo vo = new SdkFamilyVo();
            BeanUtils.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());

        return ret;
    }

}